﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Permissions;
using System.Web;
using System.Reflection;
using System.Web.Caching;
using System.Web.Hosting;

namespace Owlpal.Core.Plugins
{
    public class OwlpalPathProvider : VirtualPathProvider
    {
        private Dictionary<string, Assembly> _assemblyCache;

        public OwlpalPathProvider()
            : base()
        {
        }

        protected override void Initialize()
        {
            _assemblyCache = new Dictionary<string, Assembly>();
        }

        /// <summary>
        /// Validates if the path is within the virtual file system.
        /// </summary>
        /// <param name="virtualPath"></param>
        /// <returns></returns>
        private bool IsPathVirtual(string virtualPath)
        {
            String checkPath = VirtualPathUtility.ToAppRelative(virtualPath);
            return checkPath.StartsWith("~/Plugins", StringComparison.InvariantCultureIgnoreCase);
        }

        public override bool FileExists(string virtualPath)
        {
            if (IsPathVirtual(virtualPath))
            {
                OwlpalAssemblyFile file = (OwlpalAssemblyFile)GetFile(virtualPath);
                if (file != null)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
                return Previous.FileExists(virtualPath);
        }

        public override VirtualFile GetFile(string virtualPath)
        {
            if (IsPathVirtual(virtualPath))
                return new OwlpalAssemblyFile(virtualPath, this);
            else
                return Previous.GetFile(virtualPath);
        }

        public override CacheDependency GetCacheDependency(string virtualPath,
            System.Collections.IEnumerable virtualPathDependencies,
            DateTime utcStart)
        {
            if (IsPathVirtual(virtualPath))
            {
                System.Collections.Specialized.StringCollection fullPathDependencies = null;

                // Get the full path to all dependencies.
                foreach (string virtualDependency in virtualPathDependencies)
                {
                    if (fullPathDependencies == null)
                        fullPathDependencies = new System.Collections.Specialized.StringCollection();

                    fullPathDependencies.Add(virtualDependency);
                }
                if (fullPathDependencies == null)
                    return null;

                // Copy the list of full-path dependencies into an array.
                string[] fullPathDependenciesArray = new string[fullPathDependencies.Count];
                fullPathDependencies.CopyTo(fullPathDependenciesArray, 0);
                // Copy the virtual path into an array.
                string[] virtualPathArray = new string[1];
                virtualPathArray[0] = virtualPath;

                return new CacheDependency(virtualPathArray, fullPathDependenciesArray, utcStart);
            }
            else
                return Previous.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
        }
    }
}
